﻿//using System;
//using System.Net;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Ink;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;
//using System;
//using System.IO;
//using System.IO.IsolatedStorage;
//using System.Net;
//using System.Text;
//using System.Windows;
//using System.Windows.Media.Imaging;
//namespace myfacebookapp
//{
//    public class FBPhotoPost
//    {
//        #region class setup

//        //local property variables
//        private string ms_boundary = string.Empty;
//        private string ms_accesstoken = string.Empty;
//        private BitmapImage mimg_phototopost;
//        private string ms_caption = "WP7 Upload Photo";

//        // some multipart constants we will use in the strings we create
//        private const string Prefix = "--";
//        private const string NewLine = "\r\n";

//        public FBPhotoPost() { }


//        public BitmapImage PhotoToPost
//        {
//            get { return mimg_phototopost; }
//            set { mimg_phototopost = value; }
//        }


//        public string PhotoCaption
//        {
//            get { return ms_caption; }
//            set { ms_caption = value; }
//        }

//        public String BoundaryString
//        {
//            get
//            {
//                if (ms_boundary == string.Empty)
//                { ResetBoundaryString(); }
//                return ms_boundary;
//            }
//        }

//        public void ResetBoundaryString()
//        {
//            ms_boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
//        }

//        public String AccessToken
//        {
//            get
//            { return ms_accesstoken; }
//            set
//            { ms_accesstoken = value; }
//        }

//        #endregion

//        #region Post Data Stuff


//        public void PostPhoto()
//        //builds a byte array with the image in it and passes it to PostPhotoHTTPWebRequest
//        //which sends the data via a POST to facebook
//        {
//            if ( (ms_accesstoken != String.Empty))
//                PostPhotoHTTPWebRequest(BuildPostData());
//        }

//        private byte[] BuildPostData()
//        //Uses the PhotoToPost and BoundaryString to build a byte array we can use as the data in the POST message
//        {
//            // Build up the post message header
//            var sb = new StringBuilder();

//            //accepts a message parameter which will be the caption to go with the photo

//            sb.Append(Prefix).Append(BoundaryString).Append(NewLine);
//            sb.Append("{\n\"id\" : " + ms_accesstoken);
//            sb.Append(NewLine);
//            sb.Append("\"message\" : \"" +  Status + "\"\n");
//            sb.Append(NewLine);
//            sb.Append(NewLine);


//            byte[] ba_Header = Encoding.UTF8.GetBytes(sb.ToString());
          
//            byte[] ba_footer = Encoding.UTF8.GetBytes(String.Concat(NewLine, Prefix, BoundaryString, Prefix, NewLine));

//            byte[] data = ba_footer.

//            //return the data as a byte array
//            return postData;
//        }     
//        #endregion



//        #region HTTPWebRequest
//        //Posts the data to Facebook - can only post to me/photos (no access to users wall)

//        private void PostPhotoHTTPWebRequest(byte[] postData)
//        {
//            try
//            {
//                //Fire up an HttpWebRequest and pass in the facebook url for posting as well as the AccessToken
//                //The AccessToken has to be in the URL - it didn't work just passing it as part of the POST data
//                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://graph.facebook.com/me?access_token={0}", ms_accesstoken));
//                httpWebRequest.ContentType = String.Concat("multipart/form-data; boundary=", BoundaryString);
//                httpWebRequest.Method = "POST";

//                // start the asynchronous operation
//                httpWebRequest.BeginGetRequestStream((ar) => { GetRequestStreamCallback(ar, postData); }, httpWebRequest);
//            }
//            catch (Exception ex)
//            {
//                MessageBox.Show(ex.ToString());
//            }
//        }

//        private void GetRequestStreamCallback(IAsyncResult asynchronousResult, byte[] postData)
//        {
//            try
//            {
//                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

//                // End the operation
//                Stream postStream = request.EndGetRequestStream(asynchronousResult);

//                // Write to the request stream.
//                postStream.Write(postData, 0, postData.Length);
//                postStream.Close();

//                // Start the asynchronous operation to get the response
//                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);

//            }
//            catch (Exception ex)
//            {
//                Deployment.Current.Dispatcher.BeginInvoke(() => { UpdatePostStatus("Error Uploading [GetRequestStreamCallback]:", ex.ToString()); });
//            }
//        }

//        private void GetResponseCallback(IAsyncResult asynchronousResult)
//        {
//            try
//            {
//                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

//                // End the operation
//                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
//                Stream streamResponse = response.GetResponseStream();
//                StreamReader streamRead = new StreamReader(streamResponse);
//                string responseString = streamRead.ReadToEnd();

//                System.Diagnostics.Debug.WriteLine(responseString);
//                //Update the UI

//                Deployment.Current.Dispatcher.BeginInvoke(() => { UpdatePostStatus("Upload Success! Picture ID:", responseString); });

//                // Close the stream object
//                streamResponse.Close();
//                streamRead.Close();
//                // Release the HttpWebResponse
//                response.Close();

//            }
//            catch (Exception ex)
//            {
//                //MessageBox.Show(ex.ToString());
//                Deployment.Current.Dispatcher.BeginInvoke(() => { UpdatePostStatus("Error Uploading [GetResponseCallback]:", ex.ToString()); });
//            }
//        }
//        #endregion

//        #region ImageManipulation
//        public BitmapImage GetImageFromIsolatedStorage(String ImageFileName)
//        //Opens an image file in isolated storage and returns it as a
//        //BitmapImage object we could use as the image in the PostData method
//        {
//            {
//                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
//                {
//                    // Open the file
//                    using (IsolatedStorageFileStream isfs = isf.OpenFile(ImageFileName, FileMode.Open, FileAccess.Read))
//                    {
//                        //use the stream as the source of the Bitmap Image
//                        BitmapImage bmpimg = new BitmapImage();
//                        bmpimg.SetSource(isfs);
//                        isfs.Close();
//                        return bmpimg;
//                    }
//                }
//            }
//        }


//        private byte[] ImageToByteArray(BitmapImage bm_Image)
//        //ImageToByteArray(BitmapImage) accepts a BitmapImage object and
//        //converts it to a byte array we can use in the POST buffer
//        {
//            byte[] data;
//            {
//                // Get an Image Stream
//                MemoryStream ms_Image = new MemoryStream();
//                // write an image into the stream
//                System.Windows.Media.Imaging.Extensions.SaveJpeg(new WriteableBitmap(bm_Image), ms_Image, bm_Image.PixelWidth, bm_Image.PixelHeight, 0, 100);

//                // reset the stream pointer to the beginning
//                ms_Image.Seek(0, 0);
//                //read the stream into a byte array
//                data = new byte[ms_Image.Length];
//                ms_Image.Read(data, 0, data.Length);
//                ms_Image.Close();
//            }
//            //data now holds the bytes of the image
//            return data;
//        }


//        public BitmapImage GetBitmapImageFromUIElement(UIElement element)
//        //GetBitmapImageFromUIElement accepts a UIElement (canvas, grid, image, etc...)
//        //and builds a BitmapImage of it including any child elements
//        {
//            try
//            {
//                WriteableBitmap bmp = new WriteableBitmap((int)element.RenderSize.Width, (int)element.RenderSize.Height);
//                bmp.Render(element, null);
//                bmp.Invalidate();

//                // Get an Image Stream
//                MemoryStream ms_Image = new MemoryStream();

//                // write the image into the stream
//                bmp.SaveJpeg(ms_Image, (int)element.RenderSize.Width, (int)element.RenderSize.Height, 0, 100);
//                // reset the stream pointer to the beginning
//                ms_Image.Seek(0, 0);
//                //use the stream as the source of the Bitmap Image
//                BitmapImage bmpimg = new BitmapImage();
//                bmpimg.SetSource(ms_Image);
//                ms_Image.Close();

//                return bmpimg;
//            }
//            catch (Exception ex)
//            {
//                UpdatePostStatus("Error Getting Bitmap from UIElement:", ex.Message);
//                return null;
//            }
//        }
//        #endregion

//        #region Events code

//        //Raise the PostStatus Event so the calling code knows whats going on
//        private void UpdatePostStatus(string strHeader, string strContents)
//        {
//            if (PostStatus != null)
//            {
//                PostStatus(this, new PostStatusEventArgs(strHeader, strContents));
//            }
//        }
//        public class PostStatusEventArgs : EventArgs
//        {
//            private readonly string msg_header = string.Empty;
//            private readonly string msg_contents = string.Empty;
//            // Constructor.

//            public PostStatusEventArgs(string msgHeader, string msgContents)
//            {
//                this.msg_header = msgHeader;
//                this.msg_contents = msgContents;
//            }

//            // Properties.
//            public string MessageHeader { get { return msg_header; } }
//            public string MessageContents { get { return msg_contents; } }

//        }

//        //the PostStatus Event sends status updates
//        public delegate void PostStatusEventHandler(object sender,
//PostStatusEventArgs e);

//        public event PostStatusEventHandler PostStatus;

//        #endregion


//        public string Status { get; set; }
//    }
//}


